from toee import *

from utilities import *


def san_dialog( attachee, triggerer ):
	if (attachee.has_met(triggerer)):
		if is_daytime():
			triggerer.begin_dialog(attachee,50)
		else:
			triggerer.begin_dialog(attachee,300)
	else:
		triggerer.begin_dialog(attachee,1)
		attachee.obj_set_int(obj_f_npc_pad_i_5, 1)
	return SKIP_DEFAULT

def san_first_heartbeat(attachee, triggerer):
	attachee.obj_set_int(obj_f_npc_pad_i_5, 1)
	NPC_NighInvulnerable(attachee)
	if ( not game.combat_is_active() and attachee.obj_get_int(obj_f_npc_pad_i_5) != 0):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			obj.begin_dialog(attachee,1)
			attachee.obj_set_int(obj_f_npc_pad_i_5, 0)
			game.new_sid = 0
			return RUN_DEFAULT
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	# if ( not game.combat_is_active() and attachee.obj_get_int(obj_f_npc_pad_i_5) != 0):
		# for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			# if (is_safe_to_talk(attachee,obj)):
				# obj.begin_dialog(attachee,1)
				# attachee.obj_set_int(obj_f_npc_pad_i_5, 0)
				# return RUN_DEFAULT
	return RUN_DEFAULT

def san_enter_combat( attachee, triggerer ):
	leader = find_pc_nearest( attachee )
	attachee.turn_towards(leader)
	attachee.float_line(12057,leader)
	transform(attachee)
	if not ( attachee.has_wielded(4025) ):
		attachee.item_wield_best_all()
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if ( group_percent_hp( game.party[0] ) < 50):
		found_pc = OBJ_HANDLE_NULL
		for pc in game.party:
			if pc.type == obj_t_pc:
				if (pc.stat_level_get( stat_hp_current )) >= 0:
					found_pc = pc
				for npc in game.obj_list_vicinity( pc.location, OLC_NPC ):
					npc.ai_shitlist_remove( pc )
					npc.reaction_set( pc, 50 )
					attachee.ai_shitlist_remove( pc )
					attachee.reaction_set( pc, 50 )
		if found_pc != OBJ_HANDLE_NULL:
			found_pc.begin_dialog( attachee, 180 )
			game.new_sid = 0
			return SKIP_DEFAULT
	return RUN_DEFAULT



def Jade_Empress_Raise_Dead(attachee, pc):
	create_item_in_inventory( 6319, attachee )
	game.particles( 'Orb-Summon-Air-Elemental', attachee )
	necklace = attachee.item_find_by_proto(6239)
	if necklace != OBJ_HANDLE_NULL:
		necklace.destroy()
	sword = attachee.item_find_by_proto(4025)
	if sword != OBJ_HANDLE_NULL:
		sword.destroy()	
	# raise all PC's and PC followers
	for obj in game.obj_list_vicinity( pc.location, OLC_CRITTERS ):
		if (obj.stat_level_get(stat_hp_current) <= -10):
			obj.resurrect( CRITTER_R_CUTHBERT_RESURRECT, 0 )
		else:
			dice = dice_new("1d10+1000")
			obj.heal( OBJ_HANDLE_NULL, dice )
			obj.healsubdual( OBJ_HANDLE_NULL, dice )
	return SKIP_DEFAULT

def NPC_NighInvulnerable(attachee):
	attachee.stat_base_set(stat_strength, 24)
	attachee.stat_base_set(stat_dexterity, 24)
	attachee.stat_base_set(stat_constitution, 24)
	attachee.stat_base_set(stat_intelligence, 24)
	attachee.stat_base_set(stat_wisdom, 24)
	attachee.stat_base_set(stat_charisma, 24)
	attachee.condition_add_with_args( 'Monster Regeneration 5', 0, 0 )
	attachee.condition_add_with_args( 'Monster Subdual Immunity', 0, 0 )
	attachee.condition_add_with_args( 'Monster Energy Immunity', 'Fire', 0 )
	attachee.condition_add_with_args( 'Monster Energy Immunity', 'Cold', 0 )
	attachee.condition_add_with_args( 'Monster Energy Immunity', 'Electricity', 0 )
	attachee.condition_add_with_args( 'Monster Energy Immunity', 'Acid', 0 )
	attachee.condition_add_with_args( 'Monster Confusion Immunity', 0, 0 )
	attachee.condition_add_with_args( 'Monster Stable', 0, 0 )
	attachee.condition_add_with_args( 'Monster Untripable', 0, 0 )
	attachee.condition_add_with_args( 'Monster Plant', 0, 0 )
	attachee.condition_add_with_args( 'Monster Poison Immunity', 0, 0 )
	attachee.condition_add_with_args( 'Saving Throw Resistance Bonus', 0, 10 )
	attachee.condition_add_with_args( 'Saving Throw Resistance Bonus',1, 10 )
	attachee.condition_add_with_args( 'Saving Throw Resistance Bonus', 2, 10 )
	attachee.obj_set_int(obj_f_speed_run, 1070353216)
	return


def find_pc_nearest( obj ):
	distance = 1000000
	target = OBJ_HANDLE_NULL
	for pc in game.obj_list_vicinity( obj.location, OLC_PC ):
		if (obj.distance_to(pc) < distance):
			distance = obj.distance_to(pc)
			target = pc
	if (pc != OBJ_HANDLE_NULL):
		return pc
	return OBJ_HANDLE_NULL

def transform(attachee):
	necklace = game.obj_create( 6239, attachee.location )
	necklace.item_condition_add_with_args( 'Ring of freedom of movement', 0, 0 )
	attachee.item_get(necklace)
	sword = game.obj_create( 4025, attachee.location )
	sword.item_condition_add_with_args( 'Weapon Lawful', 0, 0 )
	sword.item_condition_add_with_args( 'Weapon Silver', 0, 0 )
	attachee.item_get(sword)
	game.particles( 'Orb-Summon-Air-Elemental', attachee )
	dress = attachee.item_find(6319)
	if dress != OBJ_HANDLE_NULL:
		dress.destroy()
	return 

